package beanstao.util.store.database;

import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import org.dom4j.Element;
import org.dom4j.Node;

import beanstao.util.Strings;
import beanstao.util.enums.DB;
import beanstao.util.setting.Setting;
import beanstao.util.store.database.impl.MysqlDbCreator;
import beanstao.util.store.database.impl.MysqlSqls;
import beanstao.util.store.database.impl.OracleDbCreator;
import beanstao.util.store.database.impl.OracleSqls;
import beanstao.util.store.database.interfaces.IDbCreator;
import beanstao.util.store.database.interfaces.IDbVerify;
import beanstao.util.store.database.interfaces.ISqls;
import beanstao.util.xml.XDocument;


public class DbSetting extends Setting<Element>
{
	private DB _DbType = DB.MYSQL;
	private String _Name;
	private String _DatabaseDriver;
	private String _DatabaseConnection;
	private String _DbUser;
	private String _DbPassword;

	private String _tablespace = " ";

	private boolean _IsMemTable = false;

	private Properties _ConfigProperties = null;

	private IDbCreator _Creator;
	private IDbVerify _DbVerify;
	private ISqls _Sqls;

	public DB getDbType()
	{
		return _DbType;
	}

	public String getName()
	{
		return _Name;
	}

	public Properties get_ConfigProperties()
	{
		return _ConfigProperties;
	}

	public void set_ConfigProperties(Properties configProperties)
	{
		_ConfigProperties = configProperties;
	}

	public String getDatabaseDriver()
	{
		return _DatabaseDriver;
	}

	public String getDatabaseConnection()
	{
		return _DatabaseConnection;
	}

	public String getDbUser()
	{
		return _DbUser;
	}

	public String getDbPassword()
	{
		return _DbPassword;
	}

	public String getDbName()
	{
		return _Name;
	}

	public String gettablespace()
	{
		return _tablespace;
	}

	public boolean isMemTable()
	{
		return _IsMemTable;
	}

	public IDbVerify getDbVerify()
	{
		return _DbVerify;
	}

	public IDbCreator getDbCreator()
	{
		return _Creator;
	}

	public ISqls getSqls()
	{
		return _Sqls;
	}

	// ----------------------------------------------

	@SuppressWarnings("unchecked")
	@Override
	protected void fillSettingData(Element settingData)
	{
		try
		{
			// 创建默认连接池属性
			// 添加连接池属性，得到节点default-config的properties属性
			// System.out.println(settingData);
			Element dcEle = (Element) settingData.selectSingleNode("bonecp-config");
			// System.out.println(dcEle);
			ArrayList<Node> proList = (ArrayList<Node>) dcEle.selectNodes("property");
			Properties defaultProperties = new Properties();
			for (Node node : proList)
			{
				if (node instanceof Element)
				{
					Element proEle = (Element) node;
					defaultProperties.put(proEle.attributeValue("name"), proEle.attributeValue("value"));
				}
			}
			this._ConfigProperties = defaultProperties;

			Node typeNode = settingData.selectSingleNode("type");

			String typename = "mysql";
			if (null != typeNode)
				typename = typeNode.getText().trim().toLowerCase();
			if (Strings.isEmpty(typename))
				typename = "mysql";

			if (typename.equals("mysql"))
				_DbType = DB.MYSQL;
			if (typename.equals("oracle"))
				_DbType = DB.ORACLE;
			this.parse(settingData, _DbType);
		}
		catch (Exception e)
		{
			this.logWarn("填充本类型的具体数据时发生异常。", e);
		}
		this.logDebug("生成一个数据库设置类型:" + this);
	}

	private void parse(Element settingElement, DB dbtype)
	{
		Map<String, String> map = XDocument.toMapByAttributes(settingElement);

		_Name = map.get("name");
		_DatabaseDriver = map.get("driver");
		_DbPassword = map.get("database_password");
		_DbUser = map.get("database_user");

		if (dbtype == DB.ORACLE)
		{
			_tablespace = map.get("tablespace");
			_Creator = new OracleDbCreator();
			_Sqls = new OracleSqls();
		}
		else
		{
			_Creator = new MysqlDbCreator();
			_Sqls = new MysqlSqls();
		}

		Node isMemNode = settingElement.selectSingleNode("isMem");
		if (null != isMemNode)
		{
			boolean bool = false;
			try
			{
				bool = Boolean.parseBoolean(isMemNode.getText().trim());
			}
			catch (Exception e)
			{
			}
			_IsMemTable = bool;
		}
		else
			_IsMemTable = false;

	}

	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("DbSetting [_DbType=");
		builder.append(_DbType);
		builder.append(", _Name=");
		builder.append(_Name);
		builder.append(", _DatabaseDriver=");
		builder.append(_DatabaseDriver);
		builder.append(", _DatabaseConnection=");
		builder.append(_DatabaseConnection);
		builder.append(", _DbUser=");
		builder.append(_DbUser);
		builder.append(", _DbPassword=");
		builder.append(_DbPassword);
		builder.append(", _tablespace=");
		builder.append(_tablespace);
		builder.append(", _IsMemTable=");
		builder.append(_IsMemTable);
		builder.append(", _Creator=");
		builder.append(_Creator);
		builder.append(", _DbVerify=");
		builder.append(_DbVerify);
		builder.append(", _Sqls=");
		builder.append(_Sqls);
		builder.append("]");
		return builder.toString();
	}

}
